/**
 * 全局公共mixin
 */
import request from '@/common/request.js';
  
module.exports = {
	data () {
		return {
			scale: 1,
			preview: false,
      current: 0,
      preview_url: '',
      status: 'loadmore',
      showSkeleton: true,
      activeItemStyle: {
        borderTop: '1rpx solid #E6E6E6'
      },
      barStyle: {
        boxShadow: `12rpx 12rpx 16rpx #01BEFF`
      },
		}
	},
  onLoad() {
    // 更新顶部导航栏信息
    this.updateCustomBarInfo()
  },
	methods: {
    // 查询数据
    searchData() {
      this.query.page = 1;
      this.list = [];
      this.getListData();
    },
    // tab选项卡切换
    changeCategory(index, id) {
      this.current = index
      this.query.page = 1;
      this.query.category_id = id;
      this.list = [];
      this.getListData();
    },
    // 获取分类数据
    getCategoryData: function() {
      let url = this.model + '/category/select';
      request.httpRequest(url).then(res => {
        this.categoryList = this.categoryList.concat(res);
      });
    },
    
    
    /**
     * 获取热门数据
     */ 
    getHotData: function(limit = 3) {
      let url = this.model + '/select';
      request.httpRequest(url, {'is_hot': 1, limit: limit}).then(res => {
        this.list = res;
      });
    },
		// 获取分页列表数据
		getListData: function() {
		  let data = {
		    title: this.query.title,
		    category_id: this.query.category_id,
		    page: this.query.page,
		    limit: this.query.limit
		  };
      
		  this.status = 'loading'
		  
      let url = this.model + '/list';
      
		  request.httpRequest(url, data).then(res => {
        if(0 == res.total) {
          this.list = [];
          this.status = 'nomore';
        } else {
          // 最后一页
          if(this.query.page > res.last_page) {
            this.status = 'nomore';
          } else {
            if(this.list.length == 0) {
              this.list = res.data;
            }
            else {
              this.list = this.list.concat(res.data);
            }
            
            this.query.page = res.current_page + 1;
            
            if(this.query.limit >=  res.total) {
              this.status = 'nomore';
            } else {
              this.status = 'loadmore';
            }
          }
        }
		  });
		},
    // 获得我的列表信息
    getUserListData: function(model) {
      let data = {
        page: this.query.page,
        limit: this.query.limit
      };
      
      this.status = 'loading'
      
      let url = model +'/list';
      
      request.httpTokenRequest(url, data).then(res => {
        if(0 == res.total) {
          this.list = [];
          this.status = 'nomore';
        } else {
          // 最后一页
          if(this.query.page > res.last_page) {
            this.status = 'nomore';
          } else {
            if(this.list.length == 0) {
              this.list = res.data;
            }
            else {
              this.list = this.list.concat(res.data);
            }
            
            this.query.page = res.current_page + 1;
            
            if(this.query.limit >=  res.total) {
              this.status = 'nomore';
            } else {
              this.status = 'loadmore';
            }
          }
        }
      });
    },
    // 获取详情数据
    getViewData: function(id) {
			let url = this.model + '/view/' + id;
      request.httpRequest(url).then(res => {
        this.data = res;
      });
    },
    goLogin() {
      uni.navigateTo({
      	url: '/pages/index?index=4'
      });
    },
    // 是否收藏
    isCollection: function(id) {
      if(!uni.getStorageSync('token')) {
        return false;
      }
			
			let url = this.memberModel + '/collection/status';
			request.httpTokenRequest(url, {id: id}, 'post').then(res => {
			  this.is_collection = res;
			});
    },
    
    // 收藏操作
    handleCollection: function(id){
      if(!uni.getStorageSync('token')) {
        this.goLogin();
        return false;
      }
      
      // 请求地址
			let url = this.memberModel+'/collection/handle';
			// 数据请求
      request.httpTokenRequest(url, {id: id}, 'post').then(res => {
          this.is_collection = res;
          if(res) {
            this.data.collection_total++;
          } else {
            this.data.collection_total--;
          }
      });
    },
    
    // 是否点赞
    isApproval: function(id) {
      if(!uni.getStorageSync('token')) {
        return false;
      }
			
			let url = this.memberModel + '/approval/status';
			request.httpTokenRequest(url, {id: id}, 'post').then(res => {
			  this.is_approval = res;
			});
    },
    
    // 点赞操作
    handleApproval: function(id){
      if(!uni.getStorageSync('token')) {
        this.goLogin();
        return false;
      }
      
			// 请求地址
      let url = this.memberModel+'/approval/handle';
      // 数据请求
      request.httpTokenRequest(url, {id: id}, 'post').then(res => {
				this.is_approval = res;
				if(res) {
					this.data.approval_total++;
				}
				else {
					this.data.approval_total--;
				}
      });
    },
    handleEnterKey: function() {
      this.setKeywordData();
      this.searchData();
    },
    // 记录关键字数据
    setKeywordData: function() {
      let data = {
        title: this.query.title,
      };
      
      let url = 'business/keyword/record';
      request.httpTokenRequest(url, data, 'post').then(res => {});
    },
    // 删除操作
    handleRemove(model, item) {
      if(!uni.getStorageSync('token')) {
        this.goLogin();
        return false;
      }
      
      // 请求地址
      let url = model +'/delete';
			// 数据请求
      request.httpTokenRequest(url, {id: item.id}, 'post').then(res => {
				let index = this.list.indexOf(item);
				if (index > -1) {
					this.list.splice(index, 1);
				}
      });
    },
    
		
    // 页面跳转函数
    goto: function(path, id = '') {
			let url = path
			if(id) {
				url = path + '?id=' + id
			}
			uni.navigateTo({
				url: url
			});
		},
    // 显示预览图片
    showPreview(url) {
      this.preview_url = url;
      this.scale = 1;
      this.preview = true;
    },
    // 隐藏预览图片
    hiddenPreview() { 
      this.preview = false;
    },
    // 点击左上角返回按钮时触发事件
		goBack() {
			// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
			const pages = getCurrentPages()
			if (pages && pages.length > 0) {
				const firstPage = pages[0]
				if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index')) {
					uni.reLaunch({
						url: '/pages/index'
					})
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			} else {
				uni.reLaunch({
					url: '/pages/index'
				})
			}
		},
		// 更新顶部导航栏信息
		async updateCustomBarInfo() {
			// 获取vuex中的自定义顶栏的高度
			let customBarHeight = this.vuex_custom_bar_height
			let statusBarHeight = this.vuex_status_bar_height
			// 如果获取失败则重新获取
			if (!customBarHeight) {
				try {
					const navBarInfo = await this.$t.updateCustomBar()
					customBarHeight = navBarInfo.customBarHeight
					statusBarHeight = navBarInfo.statusBarHeight
				} catch(e) {
					setTimeout(() => {
						this.updateCustomBarInfo()
					}, 10)
					return
				}
			}
			
			// 更新vuex中的导航栏信息
			this.$t.vuex('vuex_status_bar_height', statusBarHeight)
			this.$t.vuex('vuex_custom_bar_height', customBarHeight)
		}
	}
}
